package br.com.sgi.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;

import br.com.sgi.model.GenericModel;
import br.com.sgi.util.HibernateUtil;

public abstract class GenericDAO<T extends GenericModel> {
	private final Session session = HibernateUtil.getSession();

	public abstract Class<T> getPersistentClass();

	public Session getSession() {
		return session;
	}

	public Criteria createCriteria() {
		return getSession().createCriteria(getPersistentClass());
	}

	@SuppressWarnings("unchecked")
	public List<T> executaCriteria(Criteria criteria) {
		return criteria.list();
	}
	
	public void save(T entity) {
		getSession().save(entity);
	}

	public List<T> lista() {
		Criteria criteria = createCriteria();
		return executaCriteria(criteria);
	}
}
